//package com.xzzz.irda.guardcat.server.repos.elasticsearch;
//
//import cn.hutool.core.date.DateUtil;
//import cn.hutool.core.lang.UUID;
//import com.xzzz.common.base.util.json.JsonUtil;
//import com.xzzz.expand.es.core.EsCurdManager;
//import com.xzzz.irda.guardcat.server.app.pojo.MachineInfo;
//import com.xzzz.irda.guardcat.server.metric.MetricRepository;
//import com.xzzz.irda.guardcat.server.metric.memory.JmmInfo;
//import com.xzzz.irda.guardcat.server.repos.elasticsearch.pojo.JmmInfoEsEntity;
//import lombok.extern.slf4j.Slf4j;
//import org.elasticsearch.action.search.SearchResponse;
//import org.elasticsearch.index.query.QueryBuilder;
//import org.elasticsearch.index.query.QueryBuilders;
//import org.elasticsearch.index.reindex.BulkByScrollResponse;
//import org.elasticsearch.search.SearchHit;
//import org.elasticsearch.search.builder.SearchSourceBuilder;
//import org.elasticsearch.search.sort.SortOrder;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.annotation.Scheduled;
//
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//import java.util.Map;
//
///**
// * @author xzzz
// */
//@Slf4j
//public class JmmRepositoryInEs extends MetricRepository<JmmInfo> {
//
//    private static final String INDEX_NAME = "index_guardcat_jmm";
//
//    @Autowired
//    public EsCurdManager esCurdManager;
//
//    @Override
//    public void save(MachineInfo machineInfo, JmmInfo jmmInfo) {
//        JmmInfoEsEntity jmmInfoEsEntity = new JmmInfoEsEntity();
//        jmmInfoEsEntity.setHi(jmmInfo.getHeapUsage().getInitValue());
//        jmmInfoEsEntity.setHm(jmmInfo.getHeapUsage().getMaxValue());
//        jmmInfoEsEntity.setHc(jmmInfo.getHeapUsage().getCommittedValue());
//        jmmInfoEsEntity.setHu(jmmInfo.getHeapUsage().getUsedValue());
//        jmmInfoEsEntity.setNhi(jmmInfo.getNonHeapUsage().getInitValue());
//        jmmInfoEsEntity.setNhm(jmmInfo.getNonHeapUsage().getMaxValue());
//        jmmInfoEsEntity.setNhc(jmmInfo.getNonHeapUsage().getCommittedValue());
//        jmmInfoEsEntity.setNhu(jmmInfo.getNonHeapUsage().getUsedValue());
//        jmmInfoEsEntity.setMi(jmmInfo.getMetaspaceUsage().getInitValue());
//        jmmInfoEsEntity.setMm(jmmInfo.getMetaspaceUsage().getMaxValue());
//        jmmInfoEsEntity.setMc(jmmInfo.getMetaspaceUsage().getCommittedValue());
//        jmmInfoEsEntity.setMu(jmmInfo.getMetaspaceUsage().getUsedValue());
//        jmmInfoEsEntity.setEi(jmmInfo.getEdenSpace().getInitValue());
//        jmmInfoEsEntity.setEm(jmmInfo.getEdenSpace().getMaxValue());
//        jmmInfoEsEntity.setEc(jmmInfo.getEdenSpace().getCommittedValue());
//        jmmInfoEsEntity.setEu(jmmInfo.getEdenSpace().getUsedValue());
//        jmmInfoEsEntity.setSi(jmmInfo.getSurvivorSpace().getInitValue());
//        jmmInfoEsEntity.setSm(jmmInfo.getSurvivorSpace().getMaxValue());
//        jmmInfoEsEntity.setSc(jmmInfo.getSurvivorSpace().getCommittedValue());
//        jmmInfoEsEntity.setSu(jmmInfo.getSurvivorSpace().getUsedValue());
//        jmmInfoEsEntity.setOi(jmmInfo.getOldGen().getInitValue());
//        jmmInfoEsEntity.setOm(jmmInfo.getOldGen().getMaxValue());
//        jmmInfoEsEntity.setOc(jmmInfo.getOldGen().getCommittedValue());
//        jmmInfoEsEntity.setOu(jmmInfo.getOldGen().getUsedValue());
//
//        jmmInfoEsEntity.setCci(jmmInfo.getCodeCacheUsage().getInitValue());
//        jmmInfoEsEntity.setCcm(jmmInfo.getCodeCacheUsage().getMaxValue());
//        jmmInfoEsEntity.setCcc(jmmInfo.getCodeCacheUsage().getCommittedValue());
//        jmmInfoEsEntity.setCcu(jmmInfo.getCodeCacheUsage().getUsedValue());
//        jmmInfoEsEntity.setCcsi(jmmInfo.getCompressedClassSpace().getInitValue());
//        jmmInfoEsEntity.setCcsm(jmmInfo.getCompressedClassSpace().getMaxValue());
//        jmmInfoEsEntity.setCcsc(jmmInfo.getCompressedClassSpace().getCommittedValue());
//        jmmInfoEsEntity.setCcsu(jmmInfo.getCompressedClassSpace().getUsedValue());
//
//        jmmInfoEsEntity.setGmt(jmmInfo.getCreTime());
//        jmmInfoEsEntity.setId(UUID.fastUUID().toString(true).toUpperCase());
//        jmmInfoEsEntity.setAppName(machineInfo.getAppName());
//        jmmInfoEsEntity.setMachineId(machineInfo.getMachineId());
//
//        esCurdManager.add(INDEX_NAME, jmmInfoEsEntity);
//    }
//
//    /**
//     * 指标过期
//     */
//    @Override
//    @Scheduled(cron = "0 0 0/2 * * ?")
//    public void expire() {
//        Date today = DateUtil.date();
//        // 删除3天前的
//        Date expireDate = DateUtil.offsetDay(today, -3);
//        String expireDateStr = DateUtil.format(expireDate, "yyyy-MM-dd HH:mm:ss");
//        QueryBuilder query = QueryBuilders.rangeQuery("gmt").lt(expireDateStr);
//        BulkByScrollResponse deleteResponse = esCurdManager.deleteByQuery(INDEX_NAME, query);
//        log.warn("[GUARDCAT] 删除小于 [{}] 的JMM, 需要删除的条数: {}", expireDateStr, deleteResponse.getDeleted());
//    }
//
//    @Override
//    public JmmInfo getByMachineId(String machineId) {
//        SearchSourceBuilder searchSource = new SearchSourceBuilder().size(1).sort("gmt", SortOrder.DESC);
//        searchSource.query(QueryBuilders.termQuery("machineId", machineId));
//        SearchResponse resp = esCurdManager.search(INDEX_NAME, searchSource);
//        JmmInfoEsEntity memoryEs = null;
//        for (SearchHit hit : resp.getHits()) {
//            memoryEs = JsonUtil.toObj(hit.getSourceAsString(), JmmInfoEsEntity.class);
//        }
//
//        return memoryEsToInfo(memoryEs);
//    }
//
//    @Override
//    public List<JmmInfo> listByMachineId(String machineId) {
//        SearchSourceBuilder searchSource = new SearchSourceBuilder()
//                .size(1000)
//                .sort("gmt", SortOrder.DESC)
//                .fetchSource(new String[]{"gmt", "hc", "hu"}, null);
//
//        searchSource.query(QueryBuilders.termQuery("machineId", machineId));
//        SearchResponse resp = esCurdManager.search(INDEX_NAME, searchSource);
//
//        List<JmmInfo> list = new ArrayList<>(resp.getHits().getHits().length);
//        for (SearchHit hit : resp.getHits()) {
//            JmmInfoEsEntity memoryEs = JsonUtil.toObj(hit.getSourceAsString(), JmmInfoEsEntity.class);
//            list.add(memoryEsToInfo(memoryEs));
//        }
//        return list;
//    }
//
//    @Override
//    public List<JmmInfo> list(MachineInfo machineInfo, JmmInfo jmmInfo, Map<String, Object> args) {
//        SearchSourceBuilder searchSource = new SearchSourceBuilder()
//                .size(1000)
//                .sort("gmt", SortOrder.DESC)
//                .fetchSource(new String[]{"gmt", args.get("use").toString()}, null);
//
//        searchSource.query(QueryBuilders.termQuery("machineId", machineInfo.getMachineId()));
//        SearchResponse resp = esCurdManager.search(INDEX_NAME, searchSource);
//
//        List<JmmInfo> list = new ArrayList<>(resp.getHits().getHits().length);
//        for (SearchHit hit : resp.getHits()) {
//            JmmInfoEsEntity memoryEs = JsonUtil.toObj(hit.getSourceAsString(), JmmInfoEsEntity.class);
//            list.add(memoryEsToInfo(memoryEs));
//        }
//        return list;
//    }
//
//    private JmmInfo memoryEsToInfo(JmmInfoEsEntity es) {
//        if (es == null) {
//            return null;
//        }
//
//        JmmInfo jmmInfo = new JmmInfo();
//        JmmInfo.UsageInfo heapUsage = new JmmInfo.UsageInfo();
//        JmmInfo.UsageInfo nonHeapUsage = new JmmInfo.UsageInfo();
//        JmmInfo.UsageInfo metaspaceUsage = new JmmInfo.UsageInfo();
//        JmmInfo.UsageInfo edenSpace = new JmmInfo.UsageInfo();
//        JmmInfo.UsageInfo survivorSpace = new JmmInfo.UsageInfo();
//        JmmInfo.UsageInfo oldGen = new JmmInfo.UsageInfo();
//
//        JmmInfo.UsageInfo codeCacheUsage = new JmmInfo.UsageInfo();
//        JmmInfo.UsageInfo compressedClassSpace = new JmmInfo.UsageInfo();
//
//        heapUsage.setInitValue(es.getHi());
//        heapUsage.setMaxValue(es.getHm());
//        heapUsage.setCommittedValue(es.getHc());
//        heapUsage.setUsedValue(es.getHu());
//
//        nonHeapUsage.setInitValue(es.getNhc());
//        nonHeapUsage.setMaxValue(es.getNhm());
//        nonHeapUsage.setCommittedValue(es.getNhc());
//        nonHeapUsage.setUsedValue(es.getNhu());
//
//        metaspaceUsage.setInitValue(es.getMi());
//        metaspaceUsage.setMaxValue(es.getMm());
//        metaspaceUsage.setCommittedValue(es.getMc());
//        metaspaceUsage.setUsedValue(es.getMu());
//
//        edenSpace.setInitValue(es.getEi());
//        edenSpace.setMaxValue(es.getEm());
//        edenSpace.setCommittedValue(es.getEc());
//        edenSpace.setUsedValue(es.getEu());
//
//        survivorSpace.setInitValue(es.getSi());
//        survivorSpace.setMaxValue(es.getSm());
//        survivorSpace.setCommittedValue(es.getSc());
//        survivorSpace.setUsedValue(es.getSu());
//
//        oldGen.setInitValue(es.getOi());
//        oldGen.setMaxValue(es.getOm());
//        oldGen.setCommittedValue(es.getOc());
//        oldGen.setUsedValue(es.getOu());
//
//        codeCacheUsage.setInitValue(es.getCci());
//        codeCacheUsage.setMaxValue(es.getCcm());
//        codeCacheUsage.setCommittedValue(es.getCcc());
//        codeCacheUsage.setUsedValue(es.getCcu());
//
//        compressedClassSpace.setInitValue(es.getCcsi());
//        compressedClassSpace.setMaxValue(es.getCcsm());
//        compressedClassSpace.setCommittedValue(es.getCcsc());
//        compressedClassSpace.setUsedValue(es.getCcsu());
//
//        jmmInfo.setHeapUsage(heapUsage);
//        jmmInfo.setNonHeapUsage(nonHeapUsage);
//        jmmInfo.setMetaspaceUsage(metaspaceUsage);
//        jmmInfo.setEdenSpace(edenSpace);
//        jmmInfo.setSurvivorSpace(survivorSpace);
//        jmmInfo.setOldGen(oldGen);
//        jmmInfo.setCodeCacheUsage(codeCacheUsage);
//        jmmInfo.setCompressedClassSpace(compressedClassSpace);
//
//        jmmInfo.setCreTime(es.getGmt());
//        return jmmInfo;
//    }
//}
//
